//
//  POOutlineView.m
//  Fontcase
//
//  Created by Pieter Omvlee on 3/30/08.
//  Copyright 2008 Bohemian Coding. All rights reserved.
//

#import "CHOutlineView.h"

@implementation CHOutlineView

- (void)keyDown:(NSEvent *)anEvent
{
  if ([[self delegate] respondsToSelector:@selector(outlineView:handleKeyEvent:)]) {
    BOOL handled = [[self delegate] outlineView:self handleKeyEvent:anEvent];
    if (handled) return;
  }
  [super keyDown:anEvent];
}

- (NSRect)frameOfOutlineCellAtRow:(NSInteger)row
{
  // Default to show triangle
  BOOL showTriangle = YES;
  
  if( [[self delegate] respondsToSelector:
       @selector(outlineView:shouldShowDisclosureTriangleForItem:)] )
  {
    id item = [self itemAtRow:row];
    showTriangle = [[self delegate] outlineView:self shouldShowDisclosureTriangleForItem:item];
  }
  
  if( !showTriangle )
  {
    // If not showing triangle, return empty rect
    return NSZeroRect;
  }
  else
  {
    // else return default value
    return [super frameOfOutlineCellAtRow:row];
  }
}

- (NSMenu *)menuForEvent:(NSEvent *)theEvent
{
  if ([[self delegate] respondsToSelector:@selector(menuForEvent:)])
    return [[self delegate] menuForEvent:theEvent];
  return nil;
}

@end

@implementation NSOutlineView (NSOutlineViewAdditions)

- (id)selectedItem
{
  return [self itemAtRow:[self selectedRow]];
}

- (NSArray*)allSelectedItems
{
  NSMutableArray *items = [NSMutableArray array];
  NSEnumerator *selectedRows = [self selectedRowEnumerator];
  NSNumber *selRow = nil;
  while( (selRow = [selectedRows nextObject]) ) {
    if ([self itemAtRow:[selRow intValue]]) 
      [items addObject: [self itemAtRow:[selRow intValue]]];
  }
  return items;
}

- (void)selectItems:(NSArray*)items byExtendingSelection:(BOOL)extend
{
  unsigned int i, totalCount = [items count];
  if (extend==NO) [self deselectAll:nil];
  for (i = 0; i < totalCount; i++) {
    int row = [self rowForItem:[items objectAtIndex:i]];
    if(row>=0) [self selectRow: row byExtendingSelection:YES];
  }
}

- (void)selectItem:(id)item byExtendingSelection:(BOOL)extend
{
  [self selectRow:[self rowForItem:item] byExtendingSelection:extend];
}

- (void)selectItem:(id)item
{
  [self selectItem:item byExtendingSelection:NO];
}

@end

@implementation CHTableView

- (void)keyDown:(NSEvent *)anEvent
{
  if ([[self delegate] respondsToSelector:@selector(tableView:handleKeyEvent:)]) {
    BOOL handled = [[self delegate] tableView:self handleKeyEvent:anEvent];
    if (handled) return;
  }
  [super keyDown:anEvent];
}

- (NSMenu *)menuForEvent:(NSEvent *)theEvent
{
  if ([[self delegate] respondsToSelector:@selector(menuForEvent:)])
    return [[self delegate] menuForEvent:theEvent];
  return nil;
}

- (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
{
  if (operation == NSDragOperationNone) {
    if ([[self delegate] respondsToSelector:@selector(tableViewPoofEffect:)]) {
      if ([[self delegate] tableViewPoofEffect:self]) {
        NSShowAnimationEffect(NSAnimationEffectDisappearingItemDefault,
                              NSMakePoint(aPoint.x+[anImage size].width/2,aPoint.y+[anImage size].height/2), NSMakeSize(100,100), nil, nil, nil);
      }
    }
  }
}

- (void)dragImage:(NSImage *)anImage at:(NSPoint)imageLoc offset:(NSSize)mouseOffset event:(NSEvent *)theEvent pasteboard:(NSPasteboard *)pboard source:(id)sourceObject slideBack:(BOOL)slideBack
{
  [super dragImage:anImage at:imageLoc offset:mouseOffset event:theEvent pasteboard:pboard source:sourceObject slideBack:NO];
}


@end
